package com.gm.wholesale.bluetooth;
import android.content.Context;
import android.text.TextUtils;
import com.gm.commonlib.util.PrefsUtils.PrefUtils;
import com.gm.commonlib.util.jsonUtils.JsonUtils;
import com.gm.wholesale.bluetooth.bean.DeviceBean;
import com.gm.wholesale.bluetooth.printBlue.OrderPrintBluethManger;
import com.gm.wholesale.bluetooth.printBlue.ReceiptPrintBluethManger;
import com.gm.wholesale.bluetooth.weightBlue.WeightBlueManger;
import com.youth.banner.util.LogUtils;

public class DeviceManger {
    private static final String FILE_DEVICE = "file_device";

    private static final String DEVICE_WEIGHT_INFO = "device_weight_info";
    private static final String DEVICE_PRINT_INFO = "device_print_info";

    private static final String DEVICE_RECEIP_PRINT_INFO = "device_receip_print_info";
    private static DeviceManger deviceManger;

    public static DeviceManger getDeviceManger() {
        if (deviceManger == null) {
            deviceManger = new DeviceManger();
        }
        return deviceManger;
    }


    public void saveWeightDevice(DeviceBean deviceBean) {
        if (deviceBean==null)return;
        setString(DEVICE_WEIGHT_INFO, JsonUtils.toString(deviceBean));
    }

    public DeviceBean getWeightDevice() {
        String device = getString(DEVICE_WEIGHT_INFO);
        if (TextUtils.isEmpty(device)) {
            return null;
        }
        return JsonUtils.parse(device, DeviceBean.class);

    }

    public void savePrintDevice(DeviceBean deviceBean) {
        if (deviceBean == null) return;
        setString(DEVICE_PRINT_INFO, JsonUtils.toString(deviceBean));
    }

    public void cleanPrintDevice(){
        setString(DEVICE_PRINT_INFO, "");
    }

    public void cleanWeightDevice(){
        setString(DEVICE_WEIGHT_INFO, "");
    }

    public DeviceBean getPrintDevice() {
        String device = getString(DEVICE_PRINT_INFO);
        if (TextUtils.isEmpty(device)) {
            return null;
        }
        return JsonUtils.parse(device, DeviceBean.class);

    }

   //小票打印设备
    public void saveReceiPrintDevice( DeviceBean deviceBean) {
        if (deviceBean == null) return;
        setString(DEVICE_RECEIP_PRINT_INFO, JsonUtils.toString(deviceBean));
    }


    public DeviceBean getReceiPrintDevice() {
        String device = getString(DEVICE_RECEIP_PRINT_INFO);
        LogUtils.d("getReceiPrintDevice: " + device);
        if (TextUtils.isEmpty(device)) {
            return null;
        }
        return JsonUtils.parse(device, DeviceBean.class);

    }

    public void cleanReceiPrintDevice(){
        setString(DEVICE_RECEIP_PRINT_INFO, "");
    }

    public String getReceiPrintAddress(){
        if (getReceiPrintDevice() != null) {
            return getReceiPrintDevice().getAddress();
        }
        return null;
    }



    public String getPrintDeviceAddress() {
        if (getPrintDevice() != null) {
            return getPrintDevice().getAddress();
        }
        return null;
    }

    private void setString(String key, String value) {
        PrefUtils.setString(FILE_DEVICE, key, value);
    }

    private String getString(String key) {
        return PrefUtils.getString(FILE_DEVICE, key);
    }


    public void clear() {
        PrefUtils.clearDatas(FILE_DEVICE);
        PrefUtils.clearDatas(DEVICE_PRINT_INFO);
        PrefUtils.clearDatas(DEVICE_WEIGHT_INFO);
    }

    public static void onDestroy(Context context){
        OrderPrintBluethManger.newInstance(context).stop();
        ReceiptPrintBluethManger.newInstance(context).stop();
        WeightBlueManger.newInstance().destroy();
    }
}
